/**
 * 
 */
package com.tiduyun.data.exchange.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import com.tiduyun.data.exchange.service.GxVehicleDataAdapterService;
import com.tiduyun.data.exchange.util.ParamCheckUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wuzheng@tiduyun.com
 *
 */
@Service("mockGxVehicleDataAdapterServiceImpl")
@Slf4j
public class MockGxVehicleDataAdapterServiceImpl implements GxVehicleDataAdapterService {

    private static String[] MOCK_CHECK_BIZ_RESULT = { "EXIST", "NOT_EXIST", "FAIL" };
    private static String[] MOCK_CHECK_INVOICE_STATUS = { "正常", "作废", "红冲" };

    /**
     * @see com.tiduyun.data.exchange.service.GxVehicleDataAdapterService#getVehicleInfo(java.util.Map)
     */
    @Override
    public Map<String, Object> getVehicleInfo(Map<String, Object> params) {
        mockSleep(params);

        ParamCheckUtils.notEmptyString((String) params.get("veh_owner_name"), "veh_owner_name");
        ParamCheckUtils.notEmptyString((String) params.get("cert_no"), "cert_no");
        ParamCheckUtils.notEmptyString((String) params.get("plate_no"), "plate_no");

        Map<String, Object> result = new HashMap<>();
        result.put("biz_result", MOCK_CHECK_BIZ_RESULT[RandomUtils.nextInt(0, MOCK_CHECK_BIZ_RESULT.length)]);
        result.put("veh_owner_name", RandomStringUtils.randomAlphabetic(2, 20));
        result.put("cert_no", RandomStringUtils.randomAlphabetic(2, 20));
        result.put("plate_no", RandomStringUtils.randomAlphabetic(7, 8));
        result.put("veh_status", RandomStringUtils.randomAlphabetic(2, 20));
        result.put("engine_no", RandomStringUtils.randomAlphabetic(2, 64));
        result.put("vehicle_type", RandomStringUtils.randomAlphabetic(2, 20));
        result.put("veh_model", RandomStringUtils.randomAlphabetic(2, 20));
        result.put("brand_name", RandomStringUtils.randomAlphabetic(2, 32));
        result.put("veh_color", RandomStringUtils.randomAlphabetic(2, 32));
        result.put("first_reg_date", DateFormatUtils
                .format(new Date(System.currentTimeMillis() - RandomUtils.nextLong(3600000, 3600000000L)), "yyyy-MM-dd"));
        result.put("latest_check_date", DateFormatUtils
                .format(new Date(System.currentTimeMillis() - RandomUtils.nextLong(3600000, 3600000000L)), "yyyy-MM-dd"));

        return result;
    }

    private void mockSleep(Map<String, Object> params) {
        String mockSleepTimeValue = (String) params.get("mock_sleep");
        if (StringUtils.isEmpty(mockSleepTimeValue)) {
            return;
        }
        Long mockSleepTime = Long.valueOf(mockSleepTimeValue, 10);
        try {
            Thread.sleep(mockSleepTime);
        } catch (Exception e) {
            log.error("Mock sleep failed", e);
        }
    }

    /**
     * @see com.tiduyun.data.exchange.service.GxVehicleDataAdapterService#checkVehicleInvoice(java.util.Map)
     */
    @Override
    public Map<String, Object> checkVehicleInvoice(Map<String, Object> params) {
        mockSleep(params);

        ParamCheckUtils.notEmptyString((String) params.get("invoice_code"), "invoice_code");
        ParamCheckUtils.notEmptyString((String) params.get("invoice_no"), "invoice_no");
        ParamCheckUtils.notEmptyString((String) params.get("vat_inclusive"), "vat_inclusive");
        ParamCheckUtils.notEmptyString((String) params.get("engine_no"), "engine_no");
        ParamCheckUtils.notEmptyString((String) params.get("vin"), "vin");

        Map<String, Object> result = new HashMap<>();
        result.put("biz_result", MOCK_CHECK_BIZ_RESULT[RandomUtils.nextInt(0, MOCK_CHECK_BIZ_RESULT.length)]);
        result.put("invoice_status", MOCK_CHECK_INVOICE_STATUS[RandomUtils.nextInt(0, MOCK_CHECK_INVOICE_STATUS.length)]);
        result.put("invoice_date", DateFormatUtils
                .format(new Date(System.currentTimeMillis() - RandomUtils.nextLong(3600000, 3600000000L)), "yyyy-MM-dd"));
        result.put("adress", RandomStringUtils.randomAlphabetic(10, 50));
        return result;
    }

}
